//
//  ManyDayView.m
//  MonitoringNetwork
//
//  Created by admin on 2018/1/2.
//  Copyright © 2018年 wtmcxlm. All rights reserved.
//

#import "ManyDayView.h"
#import "UIImage+Color.h"
#import "UILabel+BasicAttributes.h"
#import "NSString+File.h"
#import "OwnerModel.h"
#import "UseModel.h"
#import "CityModel.h"
#import "ProvinceModel.h"
#import "OwnerChilderModel.h"

#import "MJExtension.h"

#import "UsedAsView.h"
#import "OwnerAsView.h"
#import "AreaASView.h"
#import "SearchDateView.h"

@interface ManyDayView()
@property (nonatomic,strong)UIView *baseView;
@property (nonatomic,strong)UIView *dimView;
@property (nonatomic,strong)UILabel *dataLabel;
@property (nonatomic,strong)UILabel *dataLabel1;
@property (nonatomic,strong)UILabel *areaLabel;
@property (nonatomic,strong)UILabel *ownnerLabel;
@property (nonatomic,strong)UILabel *useLabel;

@property (nonatomic,strong)UseModel*useModel;
@property (nonatomic,copy)NSString *areaUserId;
@property (nonatomic,copy)NSString *ownerUserId;


@property (nonatomic,copy)NSString *selectdeStarTime;
@property (nonatomic,copy)NSString *selectdeEndTime;


@property (nonatomic,strong)SearchDateView *starDateView;
@property (nonatomic,strong)SearchDateView *endDateView;
@property (nonatomic,strong)NSDateFormatter *dateFormatter;


@property (nonatomic,strong)AreaASView *areaasView;
@property (nonatomic,strong)OwnerAsView *ownerasView;
@property (nonatomic,strong)UsedAsView *useView;




@end


@implementation ManyDayView


- (AreaASView *)areaasView
{
    if (!_areaasView) {
        _areaasView = [[AreaASView alloc] init];
        _areaasView.title = @"归属地区";
        [self addSubview:_areaasView];
        [_areaasView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.bottom.mas_equalTo(self);
        }];
    }
    return _areaasView;
}
- (OwnerAsView *)ownerasView
{
    if (!_ownerasView) {
        _ownerasView = [[OwnerAsView alloc] init];
        _ownerasView.title = @"归属业主";
        [self addSubview:_ownerasView];
        [_ownerasView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.bottom.mas_equalTo(self);
        }];
    }
    return _ownerasView;
}
- (UsedAsView *)useView
{
    if (!_useView) {
        _useView = [[UsedAsView alloc] init];
        _useView.title = @"车辆用途";
        [self addSubview:_useView];
        [_useView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.bottom.mas_equalTo(self);
        }];
    }
    return _useView;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipemethod:)];
        swipe.direction = UISwipeGestureRecognizerDirectionLeft;
        [self addGestureRecognizer:swipe];
        [self setUpUI];
    }
    return self;
}
- (void)swipemethod:(UISwipeGestureRecognizer *)swipe
{
    [self ViewHideFromeSupreView];
}
#pragma mark - 懒加载

- (NSDateFormatter *)dateFormatter
{
    if (!_dateFormatter) {
        _dateFormatter = [[NSDateFormatter alloc] init];
        _dateFormatter.dateFormat = @"yyyy-MM-dd";
    }
    return _dateFormatter;
}

- (NSString *)selectdeEndTime
{
    if (!_selectdeEndTime) {
        _selectdeEndTime = [NSString getDateString2];
    }
    return _selectdeEndTime;
}
- (NSString *)selectdeStarTime
{
    if (!_selectdeStarTime) {
        _selectdeStarTime = [NSString getDateString3];
    }
    return _selectdeEndTime;
}

- (UIView *)dimView
{
    if (!_dimView) {
        _dimView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
        _dimView.backgroundColor = [UIColor grayColor];
        _dimView.alpha = 0.00f;
        [self addSubview:_dimView];
    }
    return _dimView;
}
- (UIView *)baseView
{
    if (!_baseView) {
        _baseView = [[UIView alloc] initWithFrame:CGRectMake(-kScreenWidth*0.90, 0, kScreenWidth*0.90, kScreenHeight)];
        _baseView.backgroundColor = [UIColor whiteColor];
        [self addSubview:_baseView];
    }
    return _baseView;
}
#pragma mark - Set方法

- (void)layoutSubviews
{
    [super layoutSubviews];
}

- (void)setUpUI
{
    _areaLabel = [UILabel LabelWithFont:NormalFontSize andColor:[UIColor colorWithRed:0.38f green:0.38f blue:0.38f alpha:1.00f] andTextAlignment:left andString:@""];
    _ownnerLabel = [UILabel LabelWithFont:NormalFontSize andColor:[UIColor colorWithRed:0.38f green:0.38f blue:0.38f alpha:1.00f] andTextAlignment:left andString:@""];
    _ownnerLabel.adjustsFontSizeToFitWidth = YES;
    _useLabel = [UILabel LabelWithFont:NormalFontSize andColor:[UIColor colorWithRed:0.38f green:0.38f blue:0.38f alpha:1.00f] andTextAlignment:left andString:@""];
    _dataLabel = [UILabel LabelWithFont:NormalFontSize andColor:[UIColor colorWithRed:0.38f green:0.38f blue:0.38f alpha:1.00f] andTextAlignment:left andString:[NSString stringWithFormat:@"%@",[NSString getDateString3]]];
    _dataLabel1 = [UILabel LabelWithFont:NormalFontSize andColor:[UIColor colorWithRed:0.38f green:0.38f blue:0.38f alpha:1.00f] andTextAlignment:left andString:[NSString stringWithFormat:@"%@",[NSString getDateString2]]];
    
    [UIView animateWithDuration:0.5 animations:^{
        self.dimView.frame =   CGRectMake(kScreenWidth*0.90, 0, kScreenWidth*0.10, kScreenHeight);
        self.baseView.frame =  CGRectMake(0, 0, kScreenWidth*0.90, kScreenHeight);
        self.dimView.alpha = 0.70f;
    }];
    //返回
    UIButton *backBtn = [UIButton new];
    UIImage *btnImage = [UIImage imageNamed:@"backNormal"];
    [backBtn.imageView setContentMode:UIViewContentModeScaleAspectFill];
    [backBtn setTitle:@"返回" forState:UIControlStateNormal];
    backBtn.titleLabel.font = [UIFont systemFontOfSize:BigFontSize];
    backBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    [backBtn setTitleColor:[UIColor colorWithRed:0.14f green:0.49f blue:0.85f alpha:1.00f] forState:UIControlStateNormal];
    [backBtn setImage:[btnImage imageWithColor:[UIColor colorWithRed:0.14f green:0.49f blue:0.85f alpha:1.00f]] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backBtnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    [_baseView addSubview:backBtn];
    [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_baseView.mas_left).offset(kWidthOfPt(55));
        make.top.mas_equalTo(_baseView.mas_top).offset(kHeightOfPt(86));
        make.height.mas_equalTo(20);
    }];
    
    //起始日期
    UIView *dataView = [UIView new];
    dataView.layer.cornerRadius = kWidthOfPt(5);
    dataView.clipsToBounds = YES;
    dataView.layer.borderWidth = kWidthOfPt(2);
    dataView.layer.borderColor = [UIColor colorWithRed:0.80f green:0.80f blue:0.80f alpha:1.00f].CGColor;
    [_baseView addSubview:dataView];
    [dataView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(kWidthOfPt(614));
        make.height.mas_equalTo(40);
        make.top.mas_equalTo(backBtn.mas_bottom).offset(kHeightOfPt(116));
        make.right.mas_equalTo(_baseView.mas_right).offset(-kWidthOfPt(90));
    }];
    
    [dataView addSubview:self.dataLabel];
    [_dataLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(dataView.mas_left).offset(12);
        make.centerY.mas_equalTo(dataView);
    }];
    
    UILabel *dataTitleLabel = [UILabel LabelWithFont:LeftBigFontSize andColor:[UIColor colorWithRed:0.14f green:0.14f blue:0.14f alpha:1.00f] andTextAlignment:left andString:@"起始日期"];
    [_baseView addSubview:dataTitleLabel];
    [dataTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(dataView);
        make.left.mas_equalTo(_baseView.mas_left).offset(kWidthOfPt(95));
    }];
    
    UIImageView *calendarImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"calendar"]];
    [dataView addSubview:calendarImageView];
    [calendarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(dataView).mas_offset(8);
        make.bottom.mas_equalTo(dataView).mas_offset(-8);
        make.width.mas_equalTo(calendarImageView.mas_height);
        make.centerY.mas_equalTo(dataView);
        make.right.mas_equalTo(dataView).offset(-kWidthOfPt(10));
    }];
    
    //结束日期
    UIView *dataView1 = [UIView new];
    dataView1.layer.cornerRadius = kWidthOfPt(5);
    dataView1.clipsToBounds = YES;
    dataView1.layer.borderWidth = kWidthOfPt(2);
    dataView1.layer.borderColor = [UIColor colorWithRed:0.80f green:0.80f blue:0.80f alpha:1.00f].CGColor;
    [_baseView addSubview:dataView1];
    [dataView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(kWidthOfPt(614));
        make.height.mas_equalTo(40);
        make.top.mas_equalTo(dataView.mas_bottom).offset(kHeightOfPt(38));
        make.right.mas_equalTo(_baseView.mas_right).offset(-kWidthOfPt(90));
    }];
    
    [dataView1 addSubview:self.dataLabel1];
    [_dataLabel1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(dataView1.mas_left).offset(12);
        make.centerY.mas_equalTo(dataView1);
    }];
    
    UILabel *dataTitleLabel1 = [UILabel LabelWithFont:LeftBigFontSize andColor:[UIColor colorWithRed:0.14f green:0.14f blue:0.14f alpha:1.00f] andTextAlignment:left andString:@"结束日期"];
    [_baseView addSubview:dataTitleLabel1];
    [dataTitleLabel1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(dataView1);
        make.left.mas_equalTo(_baseView.mas_left).offset(kWidthOfPt(95));
    }];
    
    UIImageView *calendarImageView1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"calendar"]];
    [dataView1 addSubview:calendarImageView1];
    [calendarImageView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(dataView1).mas_offset(8);
        make.bottom.mas_equalTo(dataView1).mas_offset(-8);
        make.width.mas_equalTo(calendarImageView1.mas_height);
        make.centerY.mas_equalTo(dataView1);
        make.right.mas_equalTo(dataView1).offset(-kWidthOfPt(10));
    }];
    
    
    
    //归属地区
//    UIView *areaView = [UIView new];
//    areaView.layer.cornerRadius = kWidthOfPt(5);
//    areaView.clipsToBounds = YES;
//    areaView.layer.borderWidth = kWidthOfPt(2);
//    areaView.layer.borderColor = [UIColor colorWithRed:0.80f green:0.80f blue:0.80f alpha:1.00f].CGColor;
//    [_baseView addSubview:areaView];
//    [areaView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.width.mas_equalTo(kWidthOfPt(614));
//        make.height.mas_equalTo(40);
//        make.top.mas_equalTo(dataView1.mas_bottom).offset(kHeightOfPt(38));
//        make.right.mas_equalTo(_baseView.mas_right).offset(-kWidthOfPt(90));
//    }];
//
//    [areaView addSubview:self.areaLabel];
//    [_areaLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_equalTo(_dataLabel);
//        make.centerY.mas_equalTo(areaView);
//    }];
//
//    UIImageView *areaImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"polygon"]];
//    [areaView addSubview:areaImageView];
//    [areaImageView mas_makeConstraints:^(MASConstraintMaker *make) {
////        make.top.mas_equalTo(areaView).mas_offset(3);
////        make.bottom.mas_equalTo(areaView).mas_offset(-3);
////        make.width.mas_equalTo(calendarImageView.mas_height);
//        make.centerY.mas_equalTo(areaView);
//        make.right.mas_equalTo(areaView).offset(-kWidthOfPt(10));
//    }];
//
//    UILabel *areaTitleLabel = [UILabel LabelWithFont:LeftBigFontSize andColor:[UIColor colorWithRed:0.14f green:0.14f blue:0.14f alpha:1.00f] andTextAlignment:left andString:@"归属地区"];
//    [_baseView addSubview:areaTitleLabel];
//    [areaTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerY.mas_equalTo(areaView);
//        make.left.mas_equalTo(_baseView.mas_left).offset(kWidthOfPt(95));
//    }];
    
    //归属业主
    UIView *ownerView = [UIView new];
    ownerView.layer.cornerRadius = kWidthOfPt(5);
    ownerView.clipsToBounds = YES;
    ownerView.layer.borderWidth = kWidthOfPt(2);
    ownerView.layer.borderColor = [UIColor colorWithRed:0.80f green:0.80f blue:0.80f alpha:1.00f].CGColor;
    [_baseView addSubview:ownerView];
    [ownerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(kWidthOfPt(614));
        make.height.mas_equalTo(40);
        make.top.mas_equalTo(dataView1.mas_bottom).offset(kHeightOfPt(38));
        make.right.mas_equalTo(_baseView.mas_right).offset(-kWidthOfPt(90));
    }];
    
    [ownerView addSubview:self.ownnerLabel];
    [_ownnerLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_dataLabel);
        make.right.mas_equalTo(calendarImageView.mas_left).offset(kWidthOfPt(10));
        make.centerY.mas_equalTo(ownerView);
    }];
    
    UIImageView *ownerImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"polygon"]];
    [ownerView addSubview:ownerImageView];
    [ownerImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(ownerView).mas_offset(8);
        make.bottom.mas_equalTo(ownerView).mas_offset(-8);
        make.width.mas_equalTo(calendarImageView.mas_height);
        make.centerY.mas_equalTo(ownerView);
        make.right.mas_equalTo(ownerView).offset(-kWidthOfPt(10));
    }];
    
    UILabel *ownerTitleLabel = [UILabel LabelWithFont:LeftBigFontSize andColor:[UIColor colorWithRed:0.14f green:0.14f blue:0.14f alpha:1.00f] andTextAlignment:left andString:@"归属业主"];
    [_baseView addSubview:ownerTitleLabel];
    [ownerTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(ownerView);
        make.left.mas_equalTo(_baseView.mas_left).offset(kWidthOfPt(95));
    }];
    
    //车辆用途
    UIView *useView = [UIView new];
    useView.layer.cornerRadius = kWidthOfPt(5);
    useView.clipsToBounds = YES;
    useView.layer.borderWidth = kWidthOfPt(2);
    useView.layer.borderColor = [UIColor colorWithRed:0.80f green:0.80f blue:0.80f alpha:1.00f].CGColor;
    [_baseView addSubview:useView];
    [useView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(kWidthOfPt(614));
        make.height.mas_equalTo(40);
        make.top.mas_equalTo(ownerView.mas_bottom).offset(kHeightOfPt(38));
        make.right.mas_equalTo(_baseView.mas_right).offset(-kWidthOfPt(90));
    }];
    
    [useView addSubview:self.useLabel];
    [_useLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_dataLabel);
        make.centerY.mas_equalTo(useView);
    }];
    
    UIImageView *useImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"polygon"]];
    [useView addSubview:useImageView];
    [useImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(useView).mas_offset(8);
        make.bottom.mas_equalTo(useView).mas_offset(-8);
        make.width.mas_equalTo(calendarImageView.mas_height);
        make.centerY.mas_equalTo(useView);
        make.right.mas_equalTo(useView).offset(-kWidthOfPt(10));
    }];
    
    UILabel *useTitleLabel = [UILabel LabelWithFont:LeftBigFontSize andColor:[UIColor colorWithRed:0.14f green:0.14f blue:0.14f alpha:1.00f] andTextAlignment:left andString:@"车辆用途"];
    [_baseView addSubview:useTitleLabel];
    [useTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(useView);
        make.left.mas_equalTo(_baseView.mas_left).offset(kWidthOfPt(95));
    }];
    
    
    //搜索按钮
    UIButton *searchBtn = [UIButton new];
    [searchBtn setTitle:@"查询" forState:UIControlStateNormal];
    searchBtn.layer.cornerRadius = 2.0f;
    searchBtn.clipsToBounds = YES;
    searchBtn.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:LagerFontSize];
    [searchBtn setBackgroundImage:[UIImage imageNamed:@"roundBtn"] forState:UIControlStateNormal];
    [searchBtn addTarget:self action:@selector(searchBtnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    [_baseView addSubview:searchBtn];
    [searchBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_baseView.mas_left).offset(kWidthOfPt(88));
        make.right.mas_equalTo(_baseView.mas_right).offset(-kHeightOfPt(88));
        make.top.mas_equalTo(useView.mas_bottom).offset(kHeightOfPt(303));
        make.height.mas_equalTo(37);
    }];
#pragma mark - 添加按钮
    UIButton *startDataBtn = [UIButton new];
    startDataBtn.tag = 100 + 1;
    [startDataBtn addTarget:self action:@selector(buttonDidClick:) forControlEvents:UIControlEventTouchUpInside];
    [dataView addSubview:startDataBtn];
    [startDataBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(dataView);
    }];
    
    UIButton *endDataBtn = [UIButton new];
    endDataBtn.tag = 100 + 2;
    [endDataBtn addTarget:self action:@selector(buttonDidClick:) forControlEvents:UIControlEventTouchUpInside];
    [dataView1 addSubview:endDataBtn];
    [endDataBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(dataView1);
    }];
    
    
//    UIButton *areaBtn = [UIButton new];
//    areaBtn.tag = 100 + 3;
//    [areaBtn addTarget:self action:@selector(buttonDidClick:) forControlEvents:UIControlEventTouchUpInside];
//    [areaView addSubview:areaBtn];
//    [areaBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.right.top.bottom.mas_equalTo(areaView);
//    }];
    
    UIButton *ownerBtn = [UIButton new];
    ownerBtn.tag = 100 + 4;
    [ownerBtn addTarget:self action:@selector(buttonDidClick:) forControlEvents:UIControlEventTouchUpInside];
    [ownerView addSubview:ownerBtn];
    [ownerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(ownerView);
    }];
    
    UIButton *userBtn = [UIButton new];
    userBtn.tag = 100 + 5;
    [userBtn addTarget:self action:@selector(buttonDidClick:) forControlEvents:UIControlEventTouchUpInside];
    [useView addSubview:userBtn];
    [userBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(useView);
    }];
    [self loadData];
}

- (void)backBtnDidClick:(UIButton *)sender
{
    [self ViewHideFromeSupreView];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self ViewHideFromeSupreView];
}

- (void)ViewHideFromeSupreView
{
    //收回view
    [UIView animateWithDuration:0.5 animations:^{
        _dimView.frame =   CGRectMake(0, 0, kScreenWidth, kScreenHeight);
        _dimView.alpha = 0.0f;
        //        self.alpha = 0.0f;
        _baseView.frame =  CGRectMake(-kScreenWidth*0.9, 0, kScreenWidth*0.9, kScreenHeight);
    } completion:^(BOOL finished) {
        if (_viewDisapper) {
            _viewDisapper();
        }
        [[NSNotificationCenter defaultCenter] postNotificationName:@"allowProhibit" object:nil];
        self.dimView.hidden = YES;
        self.baseView.hidden = YES;
        self.hidden = YES;
    }];
    
   
}
#pragma mark - 事件监听
- (void)searchBtnDidClick:(UIButton *)sender
{
   
    if (self.ownerUserId&&self.areaUserId&&self.selectdeEndTime&&self.dataLabel.text) {
        NSDictionary *param;
        if (!self.useModel.userId) {
            param = @{@"deptIds":self.ownerUserId,
                      @"areaIds":self.areaUserId,
                      @"startDate":self.dataLabel.text,
                      @"endDate":self.selectdeEndTime
                      };
        }else{
            param = @{@"deptIds":self.ownerUserId,
                      @"areaIds":self.areaUserId,
                      @"carUseIn":self.useModel.userId,
                      @"startDate":self.dataLabel.text,
                      @"endDate":self.selectdeEndTime
                      };
        }
       
        if (_searchDidClick) {
            _searchDidClick(param);
        }
         [[NSNotificationCenter defaultCenter] postNotificationName:@"allowDaysRefresh" object:nil];
        [self ViewHideFromeSupreView];
    }else{
        [YJProgressHUD showMessage:@"查询条件错误" inView:self];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [YJProgressHUD hide];
        });
    }
   
}
- (void)buttonDidClick:(UIButton *)sender
{
    
    [UIView animateWithDuration:0.3 animations:^{
        _dimView.frame = CGRectMake(kScreenWidth*0.1, 0, kScreenWidth*0.9, kScreenHeight - kScreenHeight*0.3);
    }];
    self.starDateView = [[SearchDateView alloc] init];
    self.starDateView.endDate = [self getDataWithStr:self.selectdeStarTime];
    [self didChangeStartTimeWithEndTime:self.selectdeEndTime];
    self.endDateView = [[SearchDateView alloc] init];
    self.endDateView.endDate = [self getDataWithStr:[NSString getDateString2]];
    Kweaself
    switch (sender.tag - 101) {
        case 0:
        {
           
            [self addSubview:self.starDateView];
            [self.starDateView show];
            self.starDateView.title = @"起始时间";
            self.starDateView.viewHadRemoveFromeSup = ^{
                [UIView animateWithDuration:0.3 animations:^{
                    weakSelf.dimView.frame = CGRectMake(kScreenWidth*0.1, 0, kScreenWidth*0.9, kScreenHeight);
                }];
            };
            self.starDateView.resultDate = ^(NSString *dateStr) {
                weakSelf.selectdeStarTime = dateStr;
                weakSelf.dataLabel.text = dateStr;
            };
            [self.starDateView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.top.bottom.mas_equalTo(self);
            }];
        }
            break;
        case 1:
        {
           
            [self addSubview:self.endDateView];
            [self.starDateView show];
            self.endDateView.title = @"结束时间";
            self.endDateView.viewHadRemoveFromeSup = ^{
                [UIView animateWithDuration:0.3 animations:^{
                    weakSelf.dimView.frame = CGRectMake(kScreenWidth*0.1, 0, kScreenWidth*0.9, kScreenHeight);
                }];
            };
            self.endDateView.resultDate = ^(NSString *dateStr) {
                weakSelf.selectdeEndTime = dateStr;
                [weakSelf didChangeStartTimeWithEndTime:weakSelf.selectdeEndTime];
                weakSelf.dataLabel1.text = dateStr;
            };
            [self.endDateView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.top.bottom.mas_equalTo(self);
            }];
        }
            break;
        case 2:
        {
            [self.areaasView show];
            self.areaasView.viewHadRemoveFromeSup = ^{
                [UIView animateWithDuration:0.3 animations:^{
                    weakSelf.dimView.frame = CGRectMake(kScreenWidth*0.1, 0, kScreenWidth*0.9, kScreenHeight);
                }];
            };
            self.areaasView.resultModel = ^(NSString *text,NSString *userId) {
                weakSelf.areaLabel.text = text;
                weakSelf.areaUserId = userId;
            };
            
            [self.areaasView loadAreaBottomViewWithUsedArr:self.areaArr];
        }
            break;
        case 3:
        {
            [self.ownerasView show];
            self.ownerasView.viewHadRemoveFromeSup = ^{
                [UIView animateWithDuration:0.3 animations:^{
                    weakSelf.dimView.frame = CGRectMake(kScreenWidth*0.1, 0, kScreenWidth*0.9, kScreenHeight);
                }];
            };
            self.ownerasView.ownerArr = weakSelf.ownerArr;
            self.ownerasView.resultModel = ^(NSString *text,NSString *userId) {
                weakSelf.ownnerLabel.text = text;
                weakSelf.ownerUserId = userId;
            };

            [self.ownerasView loadOwnerBottomViewWithOwnerArr:self.ownerArr];
        }
            break;
        case 4:
        {
            [self.useView show];
            self.useView.viewHadRemoveFromeSup = ^{
                [UIView animateWithDuration:0.3 animations:^{
                    weakSelf.dimView.frame = CGRectMake(kScreenWidth*0.1, 0, kScreenWidth*0.9, kScreenHeight);
                }];
            };
            self.useView.resultModel = ^(UseModel *model) {
                weakSelf.useModel = model;
                [weakSelf.useLabel setText:model.text];
            };
        
            [self.useView loadUsedBottomViewWithUsedArr:self.usedArr];
        }
            break;
        default:
            break;
    }
}
- (void)loadData
{
    [YJProgressHUD hide];
    [YJProgressHUD showProgress:@"加载中..." inView:self];
    [[NetWorkTool sharedNetworkTool] requestWithUrlString:kURL_StatisParas method:@"POST" parameters:nil progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(id  _Nullable responseObject) {
//        NSMutableArray *useArray = [NSMutableArray array];
//        NSMutableArray *ownerArray = [NSMutableArray array];
        NSMutableArray *provinceArray = [NSMutableArray array];
        if ([responseObject[@"state"] integerValue] == 0) {
            [YJProgressHUD hide];
            NSArray *carUseInListArr = responseObject[@"obj"][@"carUseInList"];
            NSArray *deptTreeArr = responseObject[@"obj"][@"deptTree"];
            NSArray *areaTreeArr = responseObject[@"obj"][@"areaTree"];
            //车辆用途
            if (carUseInListArr.count) {
//                for (NSDictionary *dic in carUseInListArr) {
//                    UseModel *model = [UseModel mj_objectWithKeyValues:dic];
//                    [useArray addObject:model];
//                }
                self.usedArr = carUseInListArr;
            }
            //归属业主
            if (deptTreeArr.count) {
//                for (NSDictionary *ownerDic in deptTreeArr) {
//                    OwnerModel *ownerModel = [OwnerModel mj_objectWithKeyValues:ownerDic];
//                    [ownerArray addObject:ownerModel];
//                }
                self.ownerArr = deptTreeArr;
            }
            //归属区域
            if (areaTreeArr.count) {
                for (NSDictionary *dic in areaTreeArr) {
                    ProvinceModel *model = [ProvinceModel mj_objectWithKeyValues:dic];
                    [provinceArray addObject:model];
                }
            }
        }
//        NSString *areafile = filePath(@"area.data");
//        [NSKeyedArchiver archiveRootObject:provinceArray toFile:areafile];
//        NSString *usefile = filePath(@"use.data");
//        [NSKeyedArchiver archiveRootObject:useArray toFile:usefile];
//        NSString *ownerfile = filePath(@"owner.data");
//        [NSKeyedArchiver archiveRootObject:self.ownerArr toFile:ownerfile];
//        self.usedArr = useArray.copy;
//        self.ownerArr = ownerArray.copy;
        self.areaArr = provinceArray.copy;
        [self firstLoadAllData];
        
    } failure:^(NSError * _Nonnull error) {
        [YJProgressHUD hide];
    }];
}
- (void)firstLoadAllData
{
    if (self.usedArr.count) {
        self.useLabel.text = @"全部";
        self.useModel.userId = @"";
    }else{
        self.useLabel.text = @"";
    }
    if (self.areaArr.count) {
        self.areaLabel.text = @"全部";
        //            NSMutableString *allAreaString = [NSMutableString string];
        //            for (int i=0; i<self.areaArr.count; i++) {
        //                ProvinceModel *model = self.areaArr[i];
        //                if (i == self.areaArr.count-1) {
        //                    [allAreaString appendFormat:@"%@",model.userId];
        //                }else{
        //                    [allAreaString appendFormat:@"%@,",model.userId];
        //                }
        //            }
        self.areaUserId = @"";
    }else{
        self.areaLabel.text = @"";
        self.areaUserId = @"";
    }
    if (self.ownerArr.count) {
        self.ownnerLabel.text = @"全部";
        //            NSMutableString *allOwnerStr = [NSMutableString string];
        //            for (int i=0; i<self.ownerArr.count; i++) {
        //                OwnerModel *model = self.ownerArr[i];
        //                if (i == self.ownerArr.count-1) {
        //                    [allOwnerStr appendFormat:@"%@",model.userId];
        //                }else{
        //                    [allOwnerStr appendFormat:@"%@,",model.userId];
        //                }
        //            }
        self.ownerUserId = @"";
    }else{
        self.ownnerLabel.text = @"";
        self.ownerUserId = @"";
    }
}
//- (void)firstLoadAllData
//{
//    if (self.usedArr.count) {
//        self.useLabel.text = @"全部";
//        self.useModel.userId = @"";
//    }else{
//        self.useLabel.text = @"";
//    }
//    if (self.areaArr.count) {
//        self.areaLabel.text = @"全部";
//        NSMutableString *allAreaString = [NSMutableString string];
//        for (int i=0; i<self.areaArr.count; i++) {
//            ProvinceModel *model = self.areaArr[i];
//            if (i == self.areaArr.count-1) {
//                [allAreaString appendFormat:@"%@",model.userId];
//            }else{
//                [allAreaString appendFormat:@"%@,",model.userId];
//            }
//        }
//        self.areaUserId = allAreaString.copy;
//    }else{
//        self.areaLabel.text = @"";
//        self.areaUserId = @"";
//    }
//    if (self.ownerArr.count) {
//        self.ownnerLabel.text = @"全部";
//        NSMutableString *allOwnerStr = [NSMutableString string];
//        for (int i=0; i<self.ownerArr.count; i++) {
//            OwnerModel *model = self.ownerArr[i];
//            if (i == self.ownerArr.count-1) {
//                [allOwnerStr appendFormat:@"%@",model.userId];
//            }else{
//                [allOwnerStr appendFormat:@"%@,",model.userId];
//            }
//        }
//        self.ownerUserId = allOwnerStr.copy;
//    }else{
//        self.ownnerLabel.text = @"";
//        self.ownerUserId = @"";
//    }
//}

- (void)didChangeStartTimeWithEndTime:(NSString *)endTime
{
    int result = [self junc_CompareOneDateStr:_selectdeStarTime withAnotherDateStr:_selectdeEndTime];
    if (result == 1) {
        NSLog(@"999");
        self.starDateView.endDate = [self getDataWithStr:self.selectdeStarTime];
    }else{
        NSDate *endDate = [self getDataWithStr:endTime];
        NSTimeInterval timeS = 1*24*60*60;
        NSDate *starDate = [[NSDate alloc] initWithTimeInterval:-timeS sinceDate:endDate];
        NSString *startStr = [self.dateFormatter stringFromDate:starDate];
        self.selectdeStarTime = startStr;
        self.selectdeStarTime = startStr;
        self.starDateView.endDate = [self getDataWithStr:startStr];
        _dataLabel.text = _selectdeStarTime;
    }
}
- (int)junc_CompareOneDateStr:(NSString *)oneDateStr withAnotherDateStr:(NSString *)anotherDateStr
{
    NSDate *dateA = [[NSDate alloc]init];
    NSDate *dateB = [[NSDate alloc]init];
    dateA = [self.dateFormatter dateFromString:oneDateStr];
    dateB = [self.dateFormatter dateFromString:anotherDateStr];
    NSComparisonResult result = [dateA compare:dateB];
    if (result == NSOrderedAscending)
    {  // oneDateStr < anotherDateStr
        return 1;
        
    }else if (result == NSOrderedDescending)
    {  // oneDateStr > anotherDateStr
        return -1;
    }
    return 0;
}

- (void)show
{
    self.hidden = NO;
    self.dimView.hidden = NO;
    self.baseView.hidden = NO;
    [UIView animateWithDuration:0.5 animations:^{
        self.dimView.frame =   CGRectMake(kScreenWidth*0.90, 0, kScreenWidth*0.10, kScreenHeight);
        self.baseView.frame =  CGRectMake(0, 0, kScreenWidth*0.90, kScreenHeight);
        self.dimView.alpha = 0.70f;
    }];
    if (!_areaArr.count) {
        [self loadData];
    }
}

- (NSDate *)getDataWithStr:(NSString *)string
{
    NSDate *resDate = [self.dateFormatter dateFromString:string];
    return resDate;
}

@end
